package com.lykj.modules.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.lykj.core.log.exception.ServiceException;
import com.lykj.core.mp.support.Condition;
import com.lykj.core.secure.utils.AuthUtil;
import com.lykj.core.tool.constant.LykjConstant;
import com.lykj.core.tool.node.ForestNodeMerger;
import com.lykj.core.tool.utils.Func;
import com.lykj.core.tool.utils.StringPool;
import com.lykj.modules.shop.entity.StoreCategory;
import com.lykj.modules.shop.vo.StoreCategoryVO;
import com.lykj.modules.shop.mapper.StoreCategoryMapper;
import com.lykj.modules.shop.service.StoreCategoryService;
import com.lykj.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;
import java.util.Map;

/**
 * 商品分类表 服务实现类
 *
 * @author cxl
 * @since 2022-01-13
 */
@Service
public class StoreCategoryServiceImpl extends BaseServiceImpl<StoreCategoryMapper, StoreCategory> implements StoreCategoryService {

	private static final String TENANT_ID = "tenantId";
	private static final String PARENT_ID = "parentId";

	@Override
	public IPage<StoreCategoryVO> selectStoreCategoryPage(IPage<StoreCategoryVO> page, StoreCategoryVO storeCategory) {
		return page.setRecords(baseMapper.selectStoreCategoryPage(page, storeCategory));
	}

	@Override
	public boolean submit(StoreCategory storeCategory) {
		if (Func.isEmpty(storeCategory.getParentId())) {
//			storeCategory.setTenantId(AuthUtil.getTenantId());
			storeCategory.setParentId(LykjConstant.TOP_PARENT_ID);
		}
		if (storeCategory.getParentId() > 0 && Func.isNull(storeCategory.getId())) {
			StoreCategory parent = getById(storeCategory.getParentId());
			if (Func.toLong(storeCategory.getParentId()) == Func.toLong(storeCategory.getId())) {
				throw new ServiceException("父节点不可选择自身!");
			}
			storeCategory.setTenantId(parent.getTenantId());
		}
		storeCategory.setIsDeleted(LykjConstant.DB_NOT_DELETED);
		return saveOrUpdate(storeCategory);
	}

	/**
	 * 懒加载分类列表
	 *
	 * @param tenantId
	 * @param parentId
	 * @param param
	 * @return
	 */
	@Override
	public List<StoreCategoryVO> lazyList(String tenantId, Long parentId, Map<String, Object> param) {
		// 设置租户ID
		if (AuthUtil.isAdministrator()) {
			tenantId = StringPool.EMPTY;
		}
		String paramTenantId = Func.toStr(param.get(TENANT_ID));
		if (Func.isNotEmpty(paramTenantId) && AuthUtil.isAdministrator()) {
			tenantId = paramTenantId;
		}
		// 判断点击搜索但是没有查询条件的情况
		if (Func.isEmpty(param.get(PARENT_ID)) && param.size() == 1) {
			parentId = 0L;
		}
		// 判断点击搜索带有查询条件的情况
		if (Func.isEmpty(param.get(PARENT_ID)) && param.size() > 1 && Func.toLong(parentId) == 0L) {
			parentId = null;
		}
		return baseMapper.lazyList(tenantId, parentId, param);
	}

	/**
	 * 树形结构
	 *
	 * @param tenantId
	 * @return
	 */
	@Override
	public List<StoreCategoryVO> tree(String tenantId) {
		return ForestNodeMerger.merge(baseMapper.tree(tenantId));
	}

}
